<?php

namespace App\Admin\Controllers\funds;

use App\Common\statement\CommonStatement;
use \App\Models\fund\Cash;
use App\Models\User;
use App\Services\im\ImService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class CashController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '提现申请';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Cash);
        $grid->model()->orderByDesc('id');
        //去除批量删除操作
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
        });

        $grid->disableCreateButton();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('uuid', 'like', "%{$this->input}%");
                    })
                        ->orWhere('id', 'like', "%{$this->input}%");

                },__('Uuid'));

            });

            $filter->column(1/3, function ($filter) {


                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('phone', 'like', "%{$this->input}%")
                            ->orWhere('name', 'like', "%{$this->input}%");
                    });

                },'昵称手机');

            });


            $filter->column(1/3, function ($filter) {
                $filter->equal('status',__('Status'))->select(CommonStatement::base_checks);
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });


        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

        $grid->column('user.name', __('Name'));

        $grid->column('account', __('提现账号'));
        $grid->column('type', __('账号类型'))->display(function ($s){
            return CommonStatement::cash_type[$s];
        });
//
        $grid->column('money', __('提现金额'));
        $grid->column('status', __('Status'))->display(function ($status){
            return CommonStatement::base_checks[$status];
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }





    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Cash::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('money', __('提现金额'));
        $show->field('account_id', __('Account id'));
        $show->field('remark', __('提现备注'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }




    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Cash);

        $form->number('user_id', __('User id'))->readonly();
        $form->text('user.name',__('Name'))->readonly();
        $form->image('user.header_img',__('header_img'))->readonly();
        $form->text('name', __('账号姓名'))->readonly();
        $form->text('account', __('提现账号'))->readonly();
        $form->select('type', '账号类型')->options(CommonStatement::cash_type)->readOnly();
        $form->decimal('money', __('提现金额'))->readonly();

        $form->select('status', '审核状态')->options(CommonStatement::base_checks);

        $form->text('remark', __('提现备注'));


        $form->saving(function (Form $form) {

            if ($form->model()->status != 2){
                $error = new MessageBag([
                    'title'   => '申请已处理,请勿重复操作...',
                ]);

                return back()->with(compact('error'));
            }

        });


        $form->saved(function (Form $form) {

            $changes = $form->model()->getChanges();

            if (isset($changes['status'])){

                if ($changes['status'] == 1){
                    //审核通过
                    $msg = '恭喜您，提现账号审核已通过';
                }else{
                    if ($form->remark){
                        $msg = sprintf('很遗憾您的提现申请 因:%s; 被驳回',$form->remark);
                    }else{
//                        $form->model()->user->

                        $msg = '很遗憾您的提现申请被驳回';
                    }

                    User::whereId($form->model()->user_id)->increment('money',$form->model()->money);
                    moneyDetailAdd($form->model()->user_id,$form->model()->money,'提现驳回');
                }
//                ImService::sendUserMsg($form->model()->user_id,$msg);

            };

        });

        return $form;
    }
}
